package com.monkeys.Utils;

import com.monkeys.pojo.Decision;
import com.monkeys.pojo.Points;
import java.util.ArrayList;
import java.util.List;
/**
 * @author KuangTeam
 */
public class CalculateUtil {

    //输入经纬度转换为 String   如: 136.542, 147.236  => “136.542,147.236” 为了方便调用百度地图API
    public static String turnLngLatToString(double lng, double lat) {
        return lng + "," + lat;
    }


    //获取质心坐标的一个
    public static Double getOneBarycenter(List<Double> coordinate, List<Double> quality) {
        double molecule = 0.0;
        Double denominator = 0.0;
        for (int i = 0; i < coordinate.size(); i++) {
            molecule += coordinate.get(i) * quality.get(i);
            denominator += quality.get(i);
        }
        return molecule / denominator;
    }


    //获取完整质心坐标  返回结果是字符串，例如： "135.6654,125.6644"
    public static String getBarycenter(List<Double> lat, List<Double> lng, List<Double> quality) {
        Double barycenterLat = getOneBarycenter(lat, quality);
        Double barycenterLng = getOneBarycenter(lng, quality);
        return turnLngLatToString(barycenterLat, barycenterLng);
    }


    //计算某个货车对某个货物的运输意愿
    //@param distanceCarGoods 车与货物之间的距离
    //@param goodsWeight 货物重量
    //@param distanceRun 货物需要运送的距离
    //@param distanceFinal 货物终点距离质心的距离
    public static double optimalDecision(double distanceCarGoods, double goodsWeight, double distanceRun, double distanceFinal) {
        Decision decision = Decision.builder()
                .distanceCarGoods(distanceCarGoods)
                .goodsWeight(goodsWeight)
                .distanceRun(distanceRun)
                .distanceFinal(distanceFinal)
                .build();

        return decision.calculateEfficiency();

    }


    //实时获取经纬度,思路：给该车辆的路程中的关键点poi组成的字符串数组,根据循环次数和现在循环到的次数i，去近似估计当前车辆的经纬度
    public static String[] getRealTimeLngLat(String[] lngLats, int cycleNumber, int i) {
        if(cycleNumber == 0)
            //如果cycleNumber = 0,代表车辆就在目的地附件,在误差范围内，直接返回最终目标的经纬度即可
        {
            return lngLats[lngLats.length - 1].split(",");
        }

        //将关键点poi分组
        int groupNum = lngLats.length / cycleNumber;
        int resultNum = groupNum * i;
        //返回字符串数组,0号为经度，1号为纬度
        if (resultNum < lngLats.length) {
            return lngLats[resultNum].split(",");
        } else
            //如果超出了范围,代表车辆提前到达,直接返回最终目标的经纬度即可
        {
            return lngLats[lngLats.length - 1].split(",");
        }
    }


    //输入节点集合，返回节点的订单号组成的字符串，比如 "123,145,167"，中间用逗号分隔
    public static String getDemandsListStr(List<Points> pointList) {
        StringBuilder stringBuilder = new StringBuilder();

        for (Points points : pointList) {
            stringBuilder.append(points.getGoodsDemandId());
            stringBuilder.append(",");
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);

        return stringBuilder.toString();
    }


    //输出Long集合，比如 "123,145,167"，输出三个Long
    public static List<Long> getDemandsListIdList(String demandsListStr) {
        List<Long> idList = new ArrayList<>();
        if (demandsListStr != null && !demandsListStr.isEmpty()) {
            String[] idArray = demandsListStr.split(",");
            for (String id : idArray) {
                Long parsedId = Long.parseLong(id);
                idList.add(parsedId);
            }
        }
        return idList;
    }
}
